order_items.js ➔ updateOrderItem   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 34
Code Lines 26

Duplication

Lines 34
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
eloc 26
dl 34
loc 34
ccs 5
cts 6
cp 0.8333
rs 9.256
c 0
b 0
f 0
cc 4
crap 4.074
1 1 View Code Duplication
const db = require("../db/database.js");
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3 1
module.exports = (function () {
4
    function addOrderItem(res, body) {
5 4
        db.run("INSERT INTO order_items (orderId, productId, amount, apiKey) VALUES (?, ?, ?, ?)",
6
            body.order_id,
7
            body.product_id,
8
            body.amount,
9
            body.api_key, (err) => {
10 4
                if (err) {
11 3
                    return res.status(500).json({
12
                        errors: {
13
                            status: 500,
14
                            source: "POST /order_item",
15
                            title: "Database error",
16
                            detail: err.message
17
                        }
18
                    });
19
                } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
20 1
                    res.status(201).json({ data: body });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
21
                }
22
            });
23
    }
24
25
    function updateOrderItem(res, body) {
26 4
        if (Number.isInteger(parseInt(body.order_id)) &&
27
            Number.isInteger(parseInt(body.product_id))) {
28 1
            db.run("UPDATE order_items SET orderId = ?, productId = ?, amount = ?" +
29
                " WHERE apiKey = ? AND orderId = ? AND productId = ?",
30
            body.order_id,
31
            body.product_id,
32
            body.amount,
33
            body.api_key,
34
            body.order_id,
35
            body.product_id, (err) => {
36 2
                if (err) {
37
                    return res.status(500).json({
38
                        errors: {
39
                            status: 500,
40
                            source: "PUT /order_item",
41
                            title: "Database error",
42
                            detail: err.message
43
                        }
44
                    });
45
                } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
46 1
                    res.status(204).send();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
47
                }
48
            });
49
        } else {
50 2
            res.status(400).json({
51
                errors: {
52
                    status: 400,
53
                    detail: "Required attributes order id and product id" +
54
                        " was not included in the request."
55
                }
56
            });
57
        }
58
    }
59
60
    function deleteOrderItem(res, body) {
61 4
        if (Number.isInteger(parseInt(body.order_id)) &&
62
            Number.isInteger(parseInt(body.product_id))) {
63 1
            db.run("DELETE FROM order_items WHERE apiKey = ? AND orderId = ? AND productId = ?",
64
                body.api_key,
65
                body.order_id,
66
                body.product_id, (err) => {
67 2
                    if (err) {
68
                        return res.status(500).json({
69
                            errors: {
70
                                status: 500,
71
                                source: "DELETE /order_item",
72
                                title: "Database error",
73
                                detail: err.message
74
                            }
75
                        });
76
                    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
77 1
                        res.status(204).send();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
78
                    }
79
                });
80
        } else {
81 2
            res.status(400).json({
82
                errors: {
83
                    status: 400,
84
                    detail: "Required attributes order id and product id" +
85
                        " was not included in the request."
86
                }
87
            });
88
        }
89
    }
90
91 1
    return {
92
93
        addOrderItem: addOrderItem,
94
        updateOrderItem: updateOrderItem,
95
        deleteOrderItem: deleteOrderItem,
96
    };
97
}());
98